<!-- Page to display a question and its corresponding answers. -->

<%@ include file="header.jsp" %>
<script type="text/javascript">
function setCurrentMenu(){
	
	document.getElementById("4").setAttribute('style', 'background:#999999');
	document.getElementById("2").setAttribute('style', 'background:#999999');
	document.getElementById("3").setAttribute('style', 'background:#999999');
	document.getElementById("1").setAttribute('style', 'background:#0093e7');
}
</script>
<%
	Questions question=(Questions)request.getAttribute("question");
	List<Answers> answers=(List<Answers>)request.getAttribute("answers");
%>
<h2> <%= question.getTitle()%> </h2>
<hr />

<!-- Displaying the description of the question -->
<br /> <%= question.getDescription()%> <br />

<!-- Displaying the tags associated with the question. -->
	<jsp:include page="RetrieveTags.jsp" flush="true">
		<jsp:param value="${question.getId()}" name="question_id"/>
	</jsp:include>
	
<!-- Checking whether the user is admin or the owner of the question.
	If yes then providing the user an option to delete the question. -->	
	<div align="right" style="padding-bottom:20px">
		<% if(session.getAttribute("adminName")!=null || (session.getAttribute("userName")!=null && ((String)session.getAttribute("userName")).equals(question.getUser_id()))){%>
		    <a title="Delete Question" href="DeleteQuestion?id=<%= question.getId()%>"><img src="images/Delete.png" width="40" height="40" alt="Delete Answer" /></a><br />
		<%} %>
		asked <%= question.getPosted()%> 
		<br />
		<%= new UsersDAO().findById(question.getUser_id()).getName()%>
		<br />
		<% if(question.isClosed()) { %><font color="#ED0E0E"><b>Question Closed</b></font><% } %>
	</div>

<!-- Displaying all the answers corresponding to the question -->
<h3> <%= answers.size()%> Answers</h3>
<hr />
<br />
<div style="padding-left:20px">
<%
   int cnt=0;
   for(Answers answer:answers){
	   if((session.getAttribute("adminName")!=null) && !(question.isClosed())){
	   } else if (answer.isApproved()) {		/* Checking whether the answer is approved or not*/
		   %>
			<div style="padding-left:0px;margin-left:0px;"><img src="images/approved.png" width="30" height="30" alt="Approved Answer"/></div>		   
		   <%
	   }
      %>
      
      <%= answer.getAnswer()%> <br />
      
      <div align="right" style="position:relative;padding-bottom:5px">
      
<!-- Checking whether the user is admin or the owner of the answer.
     If yes then providing the user an option to approve or delete the answer. -->	   
	
	<!-- Approve answer -->
	<% if(((session.getAttribute("adminName")!=null) && !(question.isClosed()) && !((String)session.getAttribute("adminName")).equals(answer.getUser_id())) || (session.getAttribute("userName")!=null && (((String)session.getAttribute("userName")).equals(question.getUser_id())) && !(question.isClosed()) && !((String)session.getAttribute("userName")).equals(answer.getUser_id()))) { %>
	  <a title="Appove this answer" alt="sdf" style="text-decoration:none" href="ApproveAnswer?id=<%=answer.getId()%>&ques_id=<%=question.getId() %>"> <img src="images/approve.png" width="40" height="40" alt="Approve Answer" /> </a>
	<% } %> 
	
	<!-- Delete answer -->      
	<% if(session.getAttribute("adminName")!=null || (session.getAttribute("userName")!=null && (((String)session.getAttribute("userName")).equals(answer.getUser_id()) || ((String)session.getAttribute("userName")).equals(question.getUser_id())))){%>
    	<a title="Delete Answer" href="DeleteAnswer?id=<%= answer.getId()%>&ques_id=<%=question.getId() %>" ><img src="images/Delete.png" width="40" height="40" alt="Delete Answer" /></a><br />
	<% } %>
	
	<!-- Displaying the time when the answer was posted -->
	answered <%= answer.getPosted()%><br/>
	
	<!-- Displaying the owner of the answer -->
	<%= new UsersDAO().findById(answer.getUser_id()).getName()%>
	<br/>&nbsp;
	<hr style="color:#AFBAB8"/>
	<%
	   cnt++;
	   if(cnt!=answers.size()){
	   %>
	   
	   <% } %>
	  </div>
      <%
   }
%>
</div>

<!-- Checking if the question is open then displaying the textbox to post the answer. -->
<%if(!question.isClosed()) { %>

<h2>Your Answer</h2><br />
<form action="InsertAnswer" method="post" name="theForm">
<input type="hidden" value="<%= question.getId()%>" name="question_id" />
<textarea id="answer" name="answer" rows="15" cols="120" style="width: 80%">
</textarea><br /><br />
 
<input type="submit" style="height:35px;width:150px;" value="Post Your Answer"/>

</form>
 <% } %>

<%@ include file="footer.jsp" %>